package info.jbohn.main;

import info.jbohn.data.Feed;
import info.jbohn.model.RSSMainModel;
import info.jbohn.rsshandling.RSSParser;
import info.jbohn.view.RSSMainUI;
import info.jbohn.view.ViewInterface;

import java.awt.EventQueue;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;


public class RSSMainController implements ControllerInterface, ListSelectionListener
{
	ViewInterface rssMainUI = null;
	RSSMainModel model = null;
	RSSParser parser = null;
	
	@Override
	public void init(ViewInterface v) 
	{
		v.register(this);
		
		model = new RSSMainModel();
		model.addObserver(v);
		
		parser = new RSSParser();
	    Feed feed = parser.readFeed("http://www.vogella.com/article.rss");
	    Feed feed1 = parser.readFeed("http://bongdaplus.vn/rss/world-cup/47.rss");
	    Feed feed2 = parser.readFeed("http://bongdaplus.vn/rss/champions-league/36.rss");
	    
	    model.addFeed(feed);
	    model.addFeed(feed1);
	    model.addFeed(feed2);
	}

	public static void main(String[] args) {
		//init metadata
		RSSMainController main = new RSSMainController();
		//launchUI
		main.launchUI();
	}
	
	@Override
	public void launchUI() 
	{
		rssMainUI = (ViewInterface) new RSSMainUI();
		init(rssMainUI);
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					rssMainUI.displayUI();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}



	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub
		Object source = e.getSource();
		if (source instanceof JList)
		{
			JList list = (JList)source;
			Object selectedValue = list.getSelectedValue();
			((RSSMainUI)rssMainUI).updateCurrentRSS(model, (String)selectedValue);
		}else
		{
			String selectedData = null;
			DefaultListSelectionModel table = (DefaultListSelectionModel)source;
			//table.getListeners(listenerType)
			//Object selectedValue = table.ge
			//((RSSMainUI)rssMainUI).updateCurrentRSSMessage(model, (String)"");
	      }
	}
	
}
